<template>
  <van-cell class="comment-item" >
    <van-image
      class="avatar"
      slot="icon"
      fit="cover"
      round
      :src="comment.aut_photo"
    />
    <div slot="title">
      <div class="title-wrap">
        <div class="name">{{ comment.aut_name }}</div>
        <div class="like-wrap">
          <van-icon
            class="like-icon"
            :color="comment.is_liking ? 'red' : '#777777'"
            :name="comment.is_liking ? 'good-job' : 'good-job-o'"
            @click="onCommentLike"
          ></van-icon>
          <span class="like-count">{{ comment.like_count }}</span>
        </div>
      </div>
      <div class="content">{{ comment.content }}</div>
      <div class="time-wrap">
        <span class="pubdate">{{ comment.pubdate | datetime('MM-DD HH:mm') }}</span>
        <van-button
          class="reply-btn"
          round
          size="mini"
          @click="onReply"
        >{{ comment.reply_count }} 回复</van-button>
      </div>
    </div>
  </van-cell>
</template>

<script>
import { addCommentLike, cancelCommentLike } from '@/api/comment.js'
export default {
  name: 'CommentItem',
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  components: {},
  data () {
    return {}
  },
  computed: {},
  watch: {},
  methods: {
    async onCommentLike () {
      const commentId = this.comment.com_id.toString()
      if (this.comment.is_liking) {
        await cancelCommentLike(commentId)
        this.comment.like_count = this.comment.like_count > 0 ? this.comment.like_count - 1 : 0
      } else {
        await addCommentLike(commentId)
        this.comment.like_count++
      }
      this.comment.is_liking = !this.comment.is_liking
    },
    onReply () {
      this.$emit('reply-click', this.comment)
    }
  },
  created () {
  },
  mounted () {
  },
  destroyed () {
  }
}
</script>

<style scoped lang='less'>
.comment-item {
  .title-wrap {
    display: flex;
    justify-content: space-between;
    .name {
      font-size: 14px;
      color: #406599;
    }
    .like-wrap {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .avatar {
    width: 36px;
    height: 36px;
    margin-right: 13px;
  }
  .content {
    font-size: 16px;
    color: #222222;
  }
  .time-wrap {
    display: flex;
    // justify-content: center;
    align-items: center;
    .pubdate {
      font-size: 10px;
      color: #222222;
      margin-right: 10px;
    }
  }
}
</style>
